// 开闭原则
// OCP：Open-Close Principle
// 
// 这里用了一个计算器的例子来解释什么是开闭原则
//

#pragma once

#include <iostream>

/// @brief 计算器虚基类
class Calculator
{
public:
	Calculator() = default;
	virtual ~Calculator() {}

	virtual int getResult() = 0;
	virtual void setParam(int a, int b) = 0;


};

/// @brief 加法计算器
class AddCalculator : public Calculator
{
public:
	AddCalculator() = default;
	virtual ~AddCalculator() {}

	virtual int getResult() {
		return a_ + b_;
	}

	virtual void setParam(int a, int b) {
		this->a_ = a;
		this->b_ = b;
	}
private:
	int a_, b_;
};

/// @brief 减法计算器
class MinusCalculator : public Calculator
{
public:
	MinusCalculator() = default;
	virtual ~MinusCalculator() {}

	virtual int getResult() {
		return a_ - b_;
	}

	virtual void setParam(int a, int b) {
		this->a_ = a;
		this->b_ = b;
	}
private:
	int a_, b_;
};

// 之后的乘法计算、除法计算
// 只要是int类型的双目运算都可以这么扩展
//

int main() {
	// 当我们需要用加法
	Calculator* calculator = new AddCalculator;
	calculator->setParam(10, 20);
	std::cout << calculator->getResult() << std::endl;

	delete calculator;

	// 当我们需要用减法
	calculator = new MinusCalculator;
	calculator->setParam(20, 10);
	std::cout << calculator->getResult() << std::endl;
	
	return 0;
}
